package cn.tj212.xiaoyin.portal.controller;

import cn.tj212.xiaoyin.core.auth.domain.AuthUser;
import cn.tj212.xiaoyin.core.auth.service.IAuthUserService;
import cn.tj212.xiaoyin.core.consts.CourseEnum;
import cn.tj212.xiaoyin.core.consts.domain.ConstsSiteCarousel;
import cn.tj212.xiaoyin.core.consts.service.IConstsSiteCarouselService;
import cn.tj212.xiaoyin.core.course.domain.Course;
import cn.tj212.xiaoyin.core.course.domain.CourseQueryDto;
import cn.tj212.xiaoyin.core.course.service.ICourseService;
import cn.tj212.xiaoyin.portal.business.IPortalBusiness;
import cn.tj212.xiaoyin.portal.vo.ConstsClassifyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 主页
 */
@Controller
@RequestMapping()
public class PortalController {

    @Autowired
    private IPortalBusiness portalBusiness;

    @Autowired
    private IConstsSiteCarouselService siteCarouselService;

    @Autowired
    private ICourseService courseService;

    @Autowired
    private IAuthUserService authUserService;

    /**
     * 首页
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView index(){
        ModelAndView mv=new ModelAndView("index");

        //加载轮播
        List<ConstsSiteCarousel> carouselList=siteCarouselService.queryCarousels(4);
        mv.addObject("carouselList",carouselList);

        //课程分类（一级分类）
        List<ConstsClassifyVO> classifys=portalBusiness.queryAllClassify();

        //课程推荐
        portalBusiness.prepareRecomdCourses(classifys);
        mv.addObject("classifys",classifys);

        //获取5门实战课推荐，按照权重（weight）进行排序
        CourseQueryDto queryEntity=new CourseQueryDto();
        queryEntity.setCount(5);//5门
        queryEntity.setFree(CourseEnum.FREE_NOT.value());//实战课程不免费
        queryEntity.descSortField("weight");//按照weight降序排序
        List<Course> actionCourseList=this.courseService.queryList(queryEntity);
        mv.addObject("actionCourseList",actionCourseList);

        //获取5门免费课推荐，按照权重（weight）进行排序
        queryEntity.setFree(CourseEnum.FREE.value());//免费课程
        queryEntity.descSortField("weight");//按照weight降序排序
        List<Course> freeCourseList=this.courseService.queryList(queryEntity);
        mv.addObject("freeCourseList",freeCourseList);

        //获取7门Java课程，根据学习人数studyCount进行排序
        queryEntity.setCount(7);
        queryEntity.setFree(null);//无所谓免费付费
        queryEntity.setSubClassify("java");//java分类
        queryEntity.descSortField("studyCount");//按照weight降序排序
        List<Course> javaCourseList=this.courseService.queryList(queryEntity);
        mv.addObject("javaCourseList",javaCourseList);

        //加载讲师
        List<AuthUser> recomdTeacherList=authUserService.queryRecomd();
        mv.addObject("recomdTeacherList",recomdTeacherList);

        return mv;
    }
}
